


# 开启WE
sflash_cmd 0x06 0x0 8

# 关闭块保护
sflash_cmd 0x01 0x0 16

# 关闭WE
sflash_cmd 0x04 0x0 8


set vv [sflash_cmd 0x05 0x0 128]
puts "read status!"
puts $vv
set arr [hex_bit_shift $vv 1]
set arr [hex_to_byte $arr 1]
puts $arr
foreach b $arr {
    set hex [format "%02x" $b]
    puts $hex;
}

# 读取05寄存器为0b11100,  BP0=1,BP1=1,BP2=1,BP3=0,BP4=0
# 读取35寄存器为s15-s8    0x00
# 读取15寄存器为s23-s16   0x22 DRV0=1

# 读取数据
proc test_flash_data {} {
    set vv [sflash_cmd $::FLASH_READ 0x0 512]
    puts "read data!"
    puts $vv
    set arr [hex_bit_shift $vv 1]
    set arr [hex_to_byte $arr 4]
    foreach b $arr {
        set hex [format "%02x" $b]
        puts $hex;
    }
}

test_flash_data

proc fpga_flash_erase_chip {} {
    sflash_cmd 0x06 0x0 8

    sflash_cmd 0x60 0x0 8

    runtest 10000
    runtest 10000
    runtest 10000
    runtest 10000

    sflash_cmd 0x04 0x0 8
    puts "erase done!"
}

proc fpga_flash_program {} {
    sflash_program_begin 0x00000000
    sflash_send32 0x12345678
    sflash_send32 0x87654321
    sflash_program_end 0xffffffff
    runtest 10000
}

#fpga_flash_erase_chip
#fpga_flash_program
#----------------
#写入测试
if [info exists chip_erase] {
    if {$::chip_erase} {
        puts "erase chip!"
        fpga_flash_erase_chip
    }
}

if [info exists chip_program] {
    if {$::chip_program} {
        fpga_flash_erase_chip
        #需要关闭 "Warn"
        set old_debug_level [debug_level] ;set e "";set old_debug_level [regsub ":" $old_debug_level $e]
        debug_level 0
        source out2.tcl
        eval $old_debug_level

        # 重置
        runtest 10000
        fpga-reset
    }
}
#----------------
#test_flash_data